#!/usr/bin/python3
# -*- coding:utf-8 -*-
# @Time   :2021/12/13
# @Author :CJX
# @File   :Injection_valve.py

import serial
import time
import binascii
import pymysql


def injection_port(injection_port):
    s = serial.Serial("/dev/ttyS0", 9600)
    con = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    address = "HCJM001"
    speed = "none"
    a1 = 0
    if injection_port == 0:
        instructions = "11050000FF008EAA"
    elif injection_port == 1:
        instructions = "11050001FF00DF6A"
    elif injection_port == 2:
        instructions = "11050002FF002F6A"
    elif injection_port == 3:
        instructions = "11050003FF007EAA"
    elif injection_port == 4:
        instructions = "11050004FF00CF6B"
    elif injection_port == 5:
        instructions = "11050005FF009EAB"
    elif injection_port == 6:
        instructions = "11050006FF006EAB"
    else:
        a1 = 10
        injection_port = "insert_database,error"
    while a1 <= 1:
        hex_str = bytes.fromhex(instructions)
        s.write(hex_str)
        n = s.inWaiting()
        a1 = a1 + 1
        if n:
            str1 = binascii.b2a_hex(s.read(n))
            str_a1 = str1[6:8]
            injection_port = str_a1.decode()
        time.sleep(1)
    s.flushInput()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany(
        "INSERT Injection_valve(address,time,injection_port,speed) VALUE(%s,%s,%s,%s)",
        [(address, time1, injection_port, speed)])
    con.commit()
    cur.close()
    return injection_port


def insert_database():
    port = 88
    speed = 88
    s = serial.Serial("/dev/ttyS0", 9600)
    con = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    address = "HCJM001"
    a1 = 0
    while a1 <= 1:
        Hex_str = bytes.fromhex("110400000002735B")
        s.write(Hex_str)
        n = s.inWaiting()
        a1 = a1 + 1
        if n:
            str1 = binascii.b2a_hex(s.read(n))
            str_a1 = str1[12:14]
            port = str_a1.decode()
            str_a2 = str1[6:8]
            speed = str_a2.decode()

        time.sleep(1)
    s.flushInput()

    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany(
        "INSERT Injection_valve(address,time,injection_port,speed) VALUE(%s,%s,%s,%s)",
        [(address, time1, port, speed)])

    cur.execute("SELECT min(id) FROM Injection_valve ")
    min_mode = int(cur.fetchone()[0])
    cur.execute("SELECT max(id) FROM Injection_valve ")
    max_mode = int(cur.fetchone()[0])
    if (max_mode - min_mode) >= 1000:
        sql2 = ("delete FROM Injection_valve " + "WHERE id <= %s " % (max_mode - 100) + " and id >= %s" % min_mode)
        cur.execute(sql2)

    con.commit()
    cur.close()
    port_speed = str(port) + str(speed)
    return port_speed


def set_speed(pattern):
    s = serial.Serial("/dev/ttyS0", 9600)
    con = pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        password='!qAz2WsX3edc',
        db='ph',
        charset='utf8'
    )
    cur = con.cursor()
    address = "HCJM001"
    injection_port = "none"
    a1 = 0
    if pattern == 1:
        instructions = "11050010FF008F6F"
    elif pattern == 2:
        instructions = "11050020FF008F60"
    elif pattern == 3:
        instructions = "11050030FF008EA5"
    else:
        a1 = 10
        pattern = "set_speed,error"
    while a1 <= 1:
        hex_str = bytes.fromhex(instructions)
        s.write(hex_str)
        n = s.inWaiting()
        a1 = a1 + 1
        if n:
            str1 = binascii.b2a_hex(s.read(n))
            str_a1 = str1[6:8]
            pattern = str_a1.decode()
        time.sleep(1)
    s.flushInput()
    time1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    cur.executemany(
        "INSERT Injection_valve(address,time,injection_port,speed) VALUE(%s,%s,%s,%s)",
        [(address, time1, injection_port, pattern)])
    con.commit()
    cur.close()
    return pattern
